const $redis = require("../lib/redis") // 你的redis配置文件路径
const $config = require('../nuxt.config.js')
const $axios =require('axios'); 
module.exports = function (Data) {
    Data.indexTopic = async function (topic_id) {
   
        let topicData = await $redis.hgetall("topic:"+topic_id);
        if (!topicData) {
            ////console.log("index error", topicData)
            return;
        }
         
       
        
        let newtopicData = {};
        for (const key in topicData) {
            if (typeof (topicData[key]) == "string" || typeof (topicData[key]) == "number") {
                newtopicData[key] = topicData[key];
            } 
        }
         
        
        // 获取用户信息
        let userData = await $redis.hgetall("user:"+topicData.uid);
        if (!userData) {
            ////console.log("index userData error", userData)
            return;
        }
        topic = {
            id: "topic_" + topic_id,
            type: "topic",
            deleted: typeof( topicData.deleted)!=="undefined"?topicData.deleted:0,
            username: userData ? userData.username : '',
            picture: userData ? userData.picture : '',
            'icon:text': userData ? userData['icon:text'] : '',
            'icon:bgColor': userData ? userData['icon:bgColor'] : '',
            uploadedpicture: userData ? userData.uploadedpicture : ''
        }
        topic = Object.assign(newtopicData, topic);

        ////console.log("index topic:", topic)
      return await $redis.lpush("snogogoDocList", JSON.stringify(topic))
    }

    Data.indexUser=  async function (uid) {
        let res = await Data.getUser(uid);
        if(res.code>0){
            console.error("indexUser:",res.msg);
            return;
        }
        let user = res.data;
       
        let newData = {};
        for (const key in user) {
            if (typeof (user[key]) == "string" || typeof (user[key]) == "number") {
                newData[key] = user[key];
            } 
        }
        let user2 = {
            id: "user_" + uid,
            type: "user",
            deleted:0,
        }
        user = Object.assign(newData, user2);
        ////console.log("index user:", user)
        return await $redis.lpush("snogogoDocList", JSON.stringify(user))
    }

    Data.indexJobs=  async function () {
        $data =  await $redis.lpop("snogogoDocList");
        let doc =  JSON.parse($data);
        if(!doc){
            return;
        }
        let id = doc.id;
        let res = await  $axios.put( $config.elasticsearchHost+'/snogogo/_doc/'+id,doc)
        if(res.code>0){
            console.error("res:",res);
        }else{
            console.log("index doc:",doc);
        }
       
    }

    







}